package cn.itcast.goods.service.impl;

import cn.itcast.constant.MessageConstant;
import cn.itcast.exception.DeletionNotAllowedException;
import cn.itcast.exception.SetmealAddFailedException;
import cn.itcast.exception.SetmealEnableFailedException;
import cn.itcast.goods.dto.SetmealDTO;
import cn.itcast.goods.dto.SetmealPageQueryDTO;
import cn.itcast.goods.mapper.DishMapper;
import cn.itcast.goods.mapper.SetmealDishMapper;
import cn.itcast.goods.mapper.SetmealMapper;
import cn.itcast.goods.pojo.Dish;
import cn.itcast.goods.pojo.Setmeal;
import cn.itcast.goods.pojo.SetmealDish;
import cn.itcast.goods.service.SetmealService;
import cn.itcast.goods.vo.DishItemVO;
import cn.itcast.goods.vo.SetmealVO;
import cn.itcast.result.PageBean;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;

    /**
     * 套餐分页查询
     *
     * @param dto
     * @return
     */
    @Override
    public PageBean pageQuery(SetmealPageQueryDTO dto) {
        PageHelper.startPage(dto.getPage(), dto.getPageSize());
        Page<SetmealVO> page = setmealMapper.pageQuery(dto);

        return new PageBean(page.getTotal(), page.getResult());
    }

    /**
     * 新增套餐
     *
     * @param dto
     */
    @Transactional
    @Override
    public void add(SetmealDTO dto) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(dto,setmeal);
        //添加套餐,别忘了主键自增返回
        setmealMapper.insert(setmeal);

        //添加菜品
        List<SetmealDish> setmealDishes = dto.getSetmealDishes();

        //开始添加
        setmealDishes.forEach(o->{
            //如果菜品中有禁售的，则抛出异常
            Long dishId = o.getDishId();

            if( dishMapper.getStartById(Arrays.asList(dishId.intValue())).size() == 0 ){
                throw new SetmealAddFailedException(MessageConstant.SETMEAL_ENABLE_ASSOCIATION_NOTSTARTED_FAILED);
            }
            //将自增的主键添加进去
            o.setSetmealId(setmeal.getId());
            setmealDishMapper.insert(o);
        });
    }

    /**
     * 批量删除套餐
     *
     * @param ids
     */
    @Override
    @Transactional
    public void delete(List<Integer> ids) {
        //先删除套餐,如果套餐为起售，则无法删除
//        List<Setmeal> setmeals = setmealDishMapper.getSetmealsByIds(ids); 为了纪念死去的代码(bug)
        //返回删除的行数
        Integer count = setmealMapper.delete(ids);
        //如果有的没有删除
        if( count != ids.size() ){
            throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
        }

        //再删除套餐与菜品的关联表
        setmealDishMapper.deleteBySetmealId(ids);
    }

    /**
     * 修改套餐
     *
     * @param dto
     */
    @Override
    @Transactional
    public void update(SetmealDTO dto) {
        Setmeal setmeal = new Setmeal();
        //修改套餐
        BeanUtils.copyProperties(dto,setmeal);
        setmealMapper.update(setmeal);

        List<SetmealDish> setmealDishes = dto.getSetmealDishes();
        setmealDishes.forEach(o->o.setSetmealId(setmeal.getId()));
        //删除关系表
        setmealDishMapper.deleteBySetmealId(Arrays.asList(dto.getId().intValue()));
        //添加关系表
        setmealDishes.forEach(o->{
            setmealDishMapper.add(o);
        });

        return;
    }

    /**
     * 根据id查询套餐及对应套餐菜品表
     *
     * @param id
     * @return
     */
    @Override
    public SetmealVO getById(Integer id) {
        //多表查询
        SetmealVO setmealVO = setmealMapper.getSetmealVOById(id);

        return setmealVO;
    }

    /**
     * 套餐起售、停售
     *
     * @param id
     * @param status
     */
    @Override
    public void startOrStop(Long id, Integer status){
        Setmeal setmeal = Setmeal.builder()
                .id(id)
                .status(status)
                .build();
        //如果起售的套餐中包含未起售的菜品，则不能起售该套餐
        List<Dish> dishes = setmealDishMapper.getDishByStemealId(id);
        dishes.forEach(o->{
            if( o.getStatus() == 0 ){
                throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_DISH_NOTSTARTED_FAILED);
            }
        });

        //有公共字段填充，不需要写修改时间，只需要加上
        // @AutoFill(OperationType.UPDATE)注解
        setmealMapper.update(setmeal);
    }

    /**
     * 根据分类id查询套餐
     *
     * @param categoryId
     * @return
     */
    @Override
    public List<Setmeal> getByCategoryId(Integer categoryId) {
        List<Setmeal> setmeals = setmealMapper.getByCategoryId(categoryId);

        return setmeals;
    }

    /**
     * 根据套餐id查询包含的菜品
     *
     * @param id
     * @return
     */
    @Override
    public List<DishItemVO> getDishItemBySetmealId(Integer id) {
        List<DishItemVO> dishItemVOS = setmealMapper.getDishItemBySetmealId(id);

        return dishItemVOS;
    }
}
